<?php

namespace Kendo\UI;
use Kendo\SerializableObject;

abstract class KWidget extends SerializableObject
{

    protected $id;
    private $attributes = array();
    private $isClientTemplate = false;

    private $assetsUrl;

    const CSS_DIR = 'styles';

    const JS_DIR = 'js';

    function __construct($id)
    {
        $this->loadBaseAssets();
        $this->id = $id;
    }


    public function attr($key, $value)
    {
        $this->attributes[$key] = $value;

        return $this;
    }

    protected function createElement()
    {
        return new \Kendo\Html\Element('div');
    }

    protected function addAttributes(\Kendo\Html\Element $element)
    {
        $element->attr('id', $this->id);

        if ($element->tagName() == 'textarea' || $element->tagName() == 'input') {
            $element->attr('name', $this->id);
        }

        foreach ($this->attributes as $key => $value) {
            $element->attr($key, $value);
        }
    }

    public function html()
    {
        $element = $this->createElement();

        $this->addAttributes($element);

        return $element->outerHtml();
    }

    protected abstract function name();

    public function render()
    {
        $output = array();

        $output[] = $this->html();
        $cs = \Yii::app()->getClientScript();
        $cs->registerScript('Kendo.KWidget#' . $this->id, $this->script(), \CClientScript::POS_END);
        return implode($output);
    }

    public function renderInTemplate()
    {
        $this->isClientTemplate = true;

        $output = $this->render();

        $this->isClientTemplate = false;

        return str_replace('</script>', '<\\/script>', $output);
    }

    private function escapeSelector($value)
    {
        return preg_replace('/([\[\]])/', "\\\\\\\\\\1", $value);
    }

    public function script($executeOnDomReady = true)
    {
        $script = array();

        if ($executeOnDomReady) {
            $script[] = 'jQuery(function(){';
        }

        $prefix = $this->isClientTemplate ? '\#' : '#';

        $script[] = 'jQuery("' . $prefix;
        $script[] = $this->escapeSelector($this->id);
        $script[] = '").kendo';
        $script[] = $this->name();
        $script[] = '(';
        $script[] = $this->toJSON();
        $script[] = ');';

        if ($executeOnDomReady) {
            $script[] = '});';
        }

        return implode($script);
    }

    protected function getAssetsUrl()
    {
        if ($this->assetsUrl === null) {
            $this->assetsUrl = \Yii::app()->getAssetManager()->publish(dirname(__FILE__) . '/../assets/');

        }
        return $this->assetsUrl;
    }

    protected function loadBaseAssets()
    {
        //$this->loadCss('kendo.default.min.css');
        //$this->loadCss('kendo.common.min.css');
        //$this->loadCss('kendo.bootstrap.min.css');
        //$this->loadJs('kendo.web.min.js');
        //$this->loadJs('cultures/kendo.culture.' . \Yii::app()->language . '.min.js');
        $cs = \Yii::app()->getClientScript();

    }

    protected function loadCss($name)
    {
        //$cs = \Yii::app()->getClientScript();
        //$cs->registerCssFile($this->getAssetsUrl() . '/' . self::CSS_DIR . '/' . $name);
    }

    protected function loadImage($name)
    {
        //\Yii::app()->assetManager->publish($this->assetsDir . self::IMAGES_DIR . '/' . $name);
    }


    protected function loadJs($name)
    {
        //$cs = \Yii::app()->getClientScript();
        //$cs->registerScriptFile($this->getAssetsUrl() . '/' . self::JS_DIR . '/' . $name, \CClientScript::POS_END);
    }
}

?>
